home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / spline.src < prev    next >
Text File  |  1991-05-29  |  5KB  |  190 lines

  1. %%HP: T(3)A(D)F(.);
  2. \<<
  3. @ Display version while working
  4. "     SPLINE V2.0
  5.   " 1 DISP
  6.  
  7.   6 CF 7 CF 8 CF 9 CF           @ Prepare flags 6-9
  8.  
  9.   IF DUP TYPE 6 SAME            @ Check if 'D1' or 'D2' are specified
  10.   THEN
  11.     CASE DUP 'D1' SAME
  12.       THEN 6 SF                 @ Set flag 6 if 'D1' specified
  13.       END DUP 'D2' SAME
  14.       THEN 7 SF                 @ Set flag 7 if 'D2' specified
  15.       END DUP \->STR ": Unknown flag" + DOERR           
  16.     END
  17.     DROP                        @ Drop 'D1' or 'D2' from stack
  18.   END
  19.  
  20.   IF DUP TYPE 5 \=/             @ See if the end-derivatives are specified
  21.   THEN { 0 0 }                  @ If not, insert {0 0} (as a place
  22. holder)
  23.   ELSE 8 SF                     @ If yes, then set flag 8
  24.   END
  25.   
  26.   IF OVER TYPE 0 SAME           @ If we have a number n in level 2 then we 
  27.   THEN OVER 2 + ROLLD           @ expect n pairs of coordinates above it
  28.   ELSE SWAP ROT                 @ Otherwise we expect two arrays in levels 2
  29.   END                           @ and 3.  In either case, we move the { s1 s2
  30. }
  31.                                 @ list form level 1 to the top of the stack.
  32.  
  33.   IF DUP TYPE 0 \=/             @ If we don't have a number in level 1
  34.   THEN 9 SF                     @ Then the coordinates are given as arrays
  35.   DUP SIZE EVAL                 @ Determine the size of array
  36.   END
  37.  
  38.                                 @ Set up the local variables
  39.      DUP 1 - { } { } 0  0   0  0 0 0 0 0   0    0   0   0
  40.   \->   n  k  x   y  h \Gl \Gm s d m a b \Gl0 \Gmn s11 s1n
  41.  
  42.   @ Begin the main program
  43.   \<<
  44.  
  45.     IF 9 FC?C
  46.     THEN 1 n                    @ Flag 9 is clear so data is in coord. pairs
  47.       FOR j                     @ Convert coordinate pairs into lists x and y
  48.           C\->R 'y' STO+ 'x' STO+
  49.       NEXT
  50.     ELSE
  51.       OBJ\-> EVAL \->LIST 'y' STO       @ Store array of y_i into the list y
  52.       OBJ\-> DROP OVER - k /            @ Compute the mesh size
  53.       0 k
  54.       FOR j
  55.         DUP j * 3 PICK + 3 ROLLD        @ Generate the x mesh
  56.       NEXT
  57.       DROP2 n \->LIST 'x' STO           @ Store the x values into the list x
  58.     END
  59.  
  60.     EVAL 's1n' STO 's11' STO            @ Read the end-derivative values
  61.  
  62.     x EVAL                              @ Compute the list of
  63.     1 k                                 @ interval lengths h_j = x_{j+1} - x_j
  64.     FOR j OVER - n ROLLD
  65.     NEXT
  66.     DROP
  67.     k \->LIST 'h' STO
  68.  
  69.     1 k                                 @ Compute the list of slopes s_j
  70.     FOR j                               @ s_j = ( y_{j+1} - y_j ) / h_j
  71.       y j GETI 3 ROLLD GET - NEG h j GET /
  72.     NEXT
  73.     k \->LIST 's' STO
  74.  
  75.     @ Compute the elements d_j of the list d:
  76.     IF 8 FS?
  77.     THEN                                @ End-derivatives are specified
  78.       1 '\Gl0' STO
  79.       1 '\Gmn' STO
  80.       s 1 GET s11 - h 1 GET / 6 *
  81.     ELSE 0
  82.     END
  83.  
  84.     @ Still computing d:
  85.     1 n 2 -
  86.     FOR j
  87.       s j GETI 3 ROLLD GET - NEG h j GETI 3 ROLLD GET + / 6 *
  88.     NEXT
  89.  
  90.     @ End of computation of d:
  91.     IF 8 FS?C
  92.     THEN
  93.       s1n s k GET - h k GET / 6 *
  94.     ELSE 0
  95.     END
  96.     n \->LIST 'd' STO
  97.  
  98.  
  99.     @ Compute lambda_j:
  100.     h OBJ\-> 1 - 1 SWAP
  101.     FOR j
  102.       DUP 3 PICK + / k ROLLD
  103.     NEXT
  104.     DROP
  105.     n 2 - \->LIST '\Gl' STO
  106.  
  107.     @ Compute gamma_j:
  108.     \Gl OBJ\-> 1 SWAP
  109.     FOR j
  110.       NEG 1 +
  111.       n 2 - ROLL
  112.     NEXT
  113.     n 2 - \->LIST '\Gm' STO
  114.  
  115.     @ Compute the moments m_j:
  116.     n IDN 2 *
  117.     2 k
  118.     FOR j
  119.       j DUP 1 - 2 \->LIST
  120.       \Gm j 1 - GET
  121.       PUT
  122.       j DUP 1 + 2 \->LIST
  123.       \Gl j 1 - GET
  124.       PUT
  125.     NEXT
  126.     2 \Gl0 PUT
  127.     n SQ 1 - \Gmn PUT
  128.     INV
  129.     d OBJ\-> \->ARRY *
  130.     'm' STO
  131.  
  132.     @ Compute a_j:
  133.     1 k
  134.     FOR j
  135.       m j GETI 3 ROLLD GET - h j GET * 6 / s j GET +
  136.     NEXT
  137.     k \->LIST 'a' STO
  138.  
  139.     @ Compute b_j:
  140.     1 k
  141.     FOR j
  142.       y j GET m j GET h j GET SQ * 6 / -
  143.     NEXT
  144.     k \->LIST 'b' STO
  145.  
  146.     @ Now we compute the individual arcs of the spline:
  147.     CASE 6 FS?C
  148.       THEN                              @ Will compute y'(x)
  149.         1 k
  150.         FOR j
  151.           m j 1 + GET 'X' x j GET - SQ *
  152.           m j GET x j 1 + GET 'X' - SQ * -
  153.           h j GET / 2 / a j GET +
  154.         NEXT
  155.       END 7 FS?C
  156.       THEN                              @ Will compute y''(x)
  157.       1 k
  158.       FOR j
  159.         m j GET x j 1 + GET 'X' - *
  160.         m j 1 + GET 'X' x j GET - * +
  161.         h j GET / NEXT
  162.       END
  163.       1 k
  164.       FOR j                             @ Will compute y(x)
  165.       m j GET x j 1 + GET 'X' - 3 ^ *
  166.       m j 1 + GET 'X' x j GET - 3 ^ * +
  167.       h j GET / 6 /
  168.       a j GET 'X' x j GET - * +
  169.       b j GET +
  170.       NEXT
  171.     END
  172.  
  173.     @ Create the output program:
  174.     "\<<\-> X\<<CASE " n ROLLD
  175.     k 2
  176.     FOR j
  177.       'X' " " + x j GET + " \>= THEN " + SWAP + " END " +
  178.       j ROLLD
  179.       -1
  180.     STEP
  181.    " END EVAL\>>\>>"
  182.  
  183.    @ Concatenate all parts:
  184.    1 n
  185.    FOR j +
  186.    NEXT
  187.    OBJ\->
  188.   \>>
  189. \>>
  190.